import axios from "axios";
import { ElMessage } from "element-plus";
import { getToken } from "./cookie";

axios.defaults.headers["Content-Type"] = "application/json;charset=utf-8";

 const service = axios.create({
  baseURL: '/dev-api',
  timeout:5000
})
//请求拦截器
service.interceptors.request.use(
   (config) => {
    if (getToken()) {
      config.headers["Authorization"] = "Bearer " + getToken();
    }
    return config;
  },
  (error) => {
    console.log(error)
    Promise.reject(error);
  }
);

//相应拦截器
service.interceptors.response.use(
  (res)=>{
      const code = res.data.code;
      const message = res.data.message;
      if(code != 1000) {
          ElMessage.error(message);
          console.log("相应数据错误处理");
          return Promise.reject(new Error(message));
      } else {
          return Promise.resolve(res.data);
      }
  },
  (error) => {
      return Promise.reject(error);
  }
);

export default service;

